#!/sbin/openrc-run
# Copyright 1999-2017 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

: ${REDSOCKS_BINARY:=/usr/sbin/redsocks}
: ${REDSOCKS_CONFIG:=/etc/redsocks.conf}
: ${REDSOCKS_PIDFILE:=/run/redsocks/redsocks.pid}

extra_commands="checkconfig"
extra_started_commands="reload"

depend() {
	need localmount
	use dns
}

checkconfig() {
	if [ ! -f "${REDSOCKS_CONFIG}" ] ; then
		eerror "No ${REDSOCKS_CONFIG} file exists!"
		return 1
	fi

        "${REDSOCKS_BINARY}" -t -c ${REDSOCKS_CONFIG} || return 1
}

start() {
        checkconfig || return 1

        ebegin "Starting ${SVCNAME}"
        start-stop-daemon --start --exec "${REDSOCKS_BINARY}" \
            -p "${REDSOCKS_PIDFILE}" -c ${REDSOCKS_CONFIG}
        eend $?
}

stop() {
        if [ "${RC_CMD}" = "restart" ] ; then
                checkconfig || return 1
        fi

        ebegin "Stopping ${SVCNAME}"
        start-stop-daemon --stop --exec "${REDSOCKS_BINARY}" \
            -p "${REDSOCKS_PIDFILE}" -c "${REDSOCKS_CONFIG}"
        eend $?
}

reload() {
        checkconfig || return 1

        ebegin "Reloading ${SVCNAME}"
        start-stop-daemon --signal HUP --exec "${REDSOCKS_BINARY}" \
	    -p "${REDSOCKS_PIDFILE}" -c "${REDSOCKS_CONFIG}"
        eend $?
}
